package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import to.Aluno;
import to.Diploma;
import to.DiplomaSituacao;
import to.Situacao;
import util.HibernateUtil;

public class DiplomaDAO {

	public List<Situacao> buscaSituacoes() {

		List<Situacao> listaSituacao = new ArrayList<Situacao>();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Situacao.class, "desc");
			criteria.addOrder(Order.asc("desc.descricao"));
			listaSituacao = (List<Situacao>) criteria.list();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return listaSituacao;
	}

	public Situacao buscaSituacaoPorCodigo(Integer s) {

		Situacao sit = new Situacao();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Situacao.class, "st");
			criteria.add(Restrictions.eq("st.id", s));
			sit = (Situacao) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return sit;
	}

	public DiplomaSituacao buscaSituacaoDoDiplomaPorCodigo(Integer s) {

		DiplomaSituacao sit = new DiplomaSituacao();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(DiplomaSituacao.class,
					"st").createAlias("st.situacao", "s");
			criteria.add(Restrictions.eq("s.id", s));
			sit = (DiplomaSituacao) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return sit;
	}

	public Diploma buscaDadosDiploma(Aluno a, Integer codCurso) {

		Diploma d = new Diploma();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Diploma.class, "dip")
					.createAlias("dip.aluno", "al")
					.createAlias("dip.curso", "c");
			criteria.add(Restrictions.eq("al.id", a.getId()));
			criteria.add(Restrictions.eq("c.idOrion", codCurso));
			d = (Diploma) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public List<DiplomaSituacao> listaSituacaoDiplomaPorAluno(Diploma d) {

		List<DiplomaSituacao> listaSituacao = new ArrayList<DiplomaSituacao>();
		Session session = null;

		try {

			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(DiplomaSituacao.class,
					"ds").createAlias("ds.diploma", "dip");
			criteria.add(Restrictions.eq("dip.id", d.getId()));
			criteria.addOrder(Order.asc("ds.data"));
			listaSituacao = (List<DiplomaSituacao>) criteria.list();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return listaSituacao;
	}

}
